using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using G4KFramework;
using G4K.Screens;

namespace G4K
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager _graphics;
        SpriteBatch _spriteBatch;

        //----------------------------------------------------
        /// Screens in Game        
        GScreen _currentScreen;
        GSplashScreen _splashScreen;
        GMenuScreen _menuScreen;
        GSongOfBirbs _sequencerSceen;
        GPuzzle _puzzleScreen;

        public Game()
        {
            _graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            // Frame rate is 30 fps by default for Windows Phone.
            TargetElapsedTime = TimeSpan.FromTicks(333333);

            _graphics.PreferredBackBufferWidth = 480;
            _graphics.PreferredBackBufferHeight = 800;

            _graphics.IsFullScreen = true;

            
        }

        #region Load & UnLoad Content
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            _spriteBatch = new SpriteBatch(GraphicsDevice);

            // Create new Screens in first load
            _splashScreen = new GSplashScreen(this, new EventHandler(SplashScreenEvent));
            _menuScreen = new GMenuScreen(this, new EventHandler(MenuScreenEvent));
            _sequencerSceen = new GSongOfBirbs(this, new EventHandler(SequencerScreenEvent));
            _puzzleScreen = new GPuzzle(this, new EventHandler(PuzzleScreenEvent));

            _currentScreen = _puzzleScreen;
        }

        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }
        #endregion

        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            _currentScreen.Update(gameTime);

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            _currentScreen.Draw();

            base.Draw(gameTime);
        }

        /// <summary>
        /// Cac event co the xay ra khi dang choi game        
        /// </summary>       
        #region Game Events
        protected override void OnDeactivated(object sender, EventArgs args)
        {
            base.OnDeactivated(sender, args);
        }
        protected override void OnActivated(object sender, EventArgs args)
        {
            base.OnActivated(sender, args);
        }
        protected override void OnExiting(object sender, EventArgs args)
        {
            base.OnExiting(sender, args);
        }
        #endregion

        /// <summary>
        /// Cac event xay ra khi thay doi Screen, 
        /// chu y khi Event nao bat len tuc la Screen do da ket thuc
        /// </summary>        
        #region Event Screen Change
        public void SplashScreenEvent(object obj, EventArgs e)
        {
            _currentScreen = _sequencerSceen;
            _splashScreen = null;
        }
        public void MenuScreenEvent(object obj, EventArgs e)
        {
            _menuScreen = null;
        }
        public void SequencerScreenEvent(object obj, EventArgs e)
        {
            _splashScreen = null;   
        }
        public void PuzzleScreenEvent(object obj, EventArgs e)
        {

        }
        #endregion
    }
}
